package com.sys.mapper;

import com.sys.dto.PlanItemWithTaskDTO;
import com.sys.entity.BizPlanItem;
import org.apache.ibatis.annotations.*;

import java.time.LocalDate;
import java.util.List;

@Mapper
public interface BizPlanItemMapper {
    @Insert("<script>" +
            "INSERT INTO biz_plan_item (plan_id, task_id, scheduled_date, scheduled_time, assigned_by, assigned_to, order_index, status, create_by, create_time, update_by, update_time, remark, del_flag) VALUES " +
            "<foreach collection='items' item='item' separator=','>" +
            "(#{item.planId}, #{item.taskId}, #{item.scheduledDate}, #{item.scheduledTime}, #{item.assignedBy}, #{item.assignedTo}, #{item.orderIndex}, COALESCE(#{item.status}, 0), #{item.createBy}, #{item.createTime}, #{item.updateBy}, #{item.updateTime}, #{item.remark}, COALESCE(#{item.delFlag}, 0))" +
            "</foreach>" +
            "</script>")
    void saveBatch(@Param("items") List<BizPlanItem> items);

    @Select("SELECT i.* FROM biz_plan_item i " +
            "JOIN biz_plan p ON i.plan_id = p.id " +
            "WHERE p.owner_user_id = #{userId} AND i.scheduled_date = #{date} AND i.del_flag = 0 " +
            "ORDER BY i.scheduled_time, i.order_index")
    List<BizPlanItem> findByUserIdAndDate(@Param("userId") Long userId, @Param("date") LocalDate date);

    @Select("SELECT i.id, i.plan_id, i.task_id, i.scheduled_date, i.scheduled_time, " +
            "i.assigned_by, i.assigned_to, i.order_index, i.status, i.remark, " +
            "t.id as task_id, t.code as task_code, t.title as task_title, " +
            "t.description as task_description, t.domain as task_domain, " +
            "t.difficulty as task_difficulty, t.age_min as task_age_min, " +
            "t.age_max as task_age_max, t.duration_minutes as task_duration_minutes, " +
            "t.recommended_frequency as task_recommended_frequency, " +
            "t.is_public as task_is_public, t.status as task_status, t.source as task_source, " +
            "t.create_by as task_create_by, t.create_time as task_create_time, " +
            "t.update_by as task_update_by, t.update_time as task_update_time, " +
            "t.remark as task_remark, t.del_flag as task_del_flag, " +
            "tp.file_path as photo_url " +
            "FROM biz_plan_item i " +
            "JOIN biz_plan p ON i.plan_id = p.id " +
            "LEFT JOIN biz_task t ON i.task_id = t.id " +
            "LEFT JOIN biz_task_log tl ON i.id = tl.plan_item_id AND tl.del_flag = 0 " +
            "LEFT JOIN biz_task_photo tp ON tl.id = tp.task_log_id AND tp.del_flag = 0 " +
            "WHERE p.owner_user_id = #{userId} AND i.scheduled_date = #{date} " +
            "AND i.del_flag = 0 AND (t.del_flag = 0 OR t.del_flag IS NULL) " +
            "ORDER BY i.scheduled_time, i.order_index")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "planId", column = "plan_id"),
            @Result(property = "taskId", column = "task_id"),
            @Result(property = "scheduledDate", column = "scheduled_date"),
            @Result(property = "scheduledTime", column = "scheduled_time"),
            @Result(property = "assignedBy", column = "assigned_by"),
            @Result(property = "assignedTo", column = "assigned_to"),
            @Result(property = "orderIndex", column = "order_index"),
            @Result(property = "status", column = "status"),
            @Result(property = "remark", column = "remark"),
            @Result(property = "task.id", column = "task_id"),
            @Result(property = "task.code", column = "task_code"),
            @Result(property = "task.title", column = "task_title"),
            @Result(property = "task.description", column = "task_description"),
            @Result(property = "task.domain", column = "task_domain"),
            @Result(property = "task.difficulty", column = "task_difficulty"),
            @Result(property = "task.ageMin", column = "task_age_min"),
            @Result(property = "task.ageMax", column = "task_age_max"),
            @Result(property = "task.durationMinutes", column = "task_duration_minutes"),
            @Result(property = "task.recommendedFrequency", column = "task_recommended_frequency"),
            @Result(property = "task.isPublic", column = "task_is_public"),
            @Result(property = "task.status", column = "task_status"),
            @Result(property = "task.source", column = "task_source"),
            @Result(property = "task.createBy", column = "task_create_by"),
            @Result(property = "task.createTime", column = "task_create_time"),
            @Result(property = "task.updateBy", column = "task_update_by"),
            @Result(property = "task.updateTime", column = "task_update_time"),
            @Result(property = "task.remark", column = "task_remark"),
            @Result(property = "task.delFlag", column = "task_del_flag"),
            @Result(property = "photoUrl", column = "photo_url")
    })
    List<PlanItemWithTaskDTO> findByUserIdAndDateWithTask(@Param("userId") Long userId, @Param("date") LocalDate date);

    @Update("UPDATE biz_plan_item SET status = #{status}, update_time = #{updateTime} WHERE id = #{id}")
    void updateStatus(@Param("id") Long id, @Param("status") Integer status, @Param("updateTime") java.time.LocalDateTime updateTime);

    @Update("UPDATE biz_plan_item SET del_flag = 1, update_time = #{updateTime} WHERE id = #{id}")
    void deletePlanItem(@Param("id") Long id, @Param("updateTime") java.time.LocalDateTime updateTime);

    @Update("UPDATE biz_plan_item SET del_flag = 1, update_time = #{updateTime} WHERE plan_id = #{planId}")
    void deleteByPlanId(@Param("planId") Long planId, @Param("updateTime") java.time.LocalDateTime updateTime);

    @Select("SELECT i.* FROM biz_plan_item i " +
            "JOIN biz_plan p ON i.plan_id = p.id " +
            "WHERE p.owner_user_id = #{userId} " +
            "AND i.scheduled_date BETWEEN #{startDate} AND #{endDate} " +
            "AND i.del_flag = 0 " +
            "ORDER BY i.scheduled_date, i.scheduled_time")
    List<BizPlanItem> findByUserIdAndDateRange(@Param("userId") Long userId, 
                                                 @Param("startDate") LocalDate startDate, 
                                                 @Param("endDate") LocalDate endDate);
}